# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=gtk3
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache")
pkgver=3.24.38.r43.g0f717ca
pkgrel=1
_commit=0f717ca4239175334ff54c72b0f0d1953d9f95b5
pkgdesc="GObject-based multi-platform GUI toolkit (v3) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.gtk.org/"
license=("spdx:LGPL-2.1-or-later")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-sassc"
             "${MINGW_PACKAGE_PREFIX}-libxslt")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-adwaita-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-atk"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-json-glib"
         "${MINGW_PACKAGE_PREFIX}-libepoxy"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-shared-mime-info")
options=('strip' '!debug' 'staticlibs')
source=("git+https://gitlab.gnome.org/GNOME/gtk.git#commit=$_commit"
        "0003-gtkwindow-Don-t-force-enable-CSD-under-Windows.patch"
        "0004-Disable-low-level-keyboard-hook.patch"
        "0005-meson-fix-linker-flags-pkgconfig.patch"
        "gtk-query-immodules-3.0.hook.in"
        "gtk-update-icon-cache.hook.in"
        "gtk-update-icon-cache.script.in")
sha256sums=('SKIP'
            'b84a7f38f0af80680bee143d431f2a7bae53899efb337de0f67a1b4d9b59fa02'
            'e553083298495f9581ae1454b1046a22b83e81862311b30de984057eec859708'
            'f006498eaf5e93a927f1ee966512f7f059e66d3f47433eedab5c0f08692bdfac'
            'adbe57eb726d882bba7a031ab8fb1788e7cd03cbf713823fd0f99d3cb380b97c'
            '56a566739c4f153f3c924b2bfe5ec7aaca469e15c023810cd7c5f57036d1a258'
            'ad431cedaa2c4a20db35c753c89b85b45c67872004255762094277d27eb7c18a')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

pkgver() {
  cd "${srcdir}/gtk"
  git describe --long --abbrev=7 | sed 's/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
  cd "${srcdir}/gtk"

  # https://gitlab.gnome.org/GNOME/gtk/issues/760
  apply_patch_with_msg \
    0003-gtkwindow-Don-t-force-enable-CSD-under-Windows.patch

  apply_patch_with_msg \
    0004-Disable-low-level-keyboard-hook.patch

  # https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/5458
  apply_patch_with_msg \
    0005-meson-fix-linker-flags-pkgconfig.patch

}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("--buildtype=release")
  else
    extra_config+=("--buildtype=debug")
  fi

  CFLAGS+=" -DG_ENABLE_DEBUG"  # so the GTK_DEBUG env var works
  CFLAGS+=" -Wno-deprecated-declarations"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson \
    --prefix="${MINGW_PREFIX}" \
    --wrap-mode=nodownload \
    --default-library=both \
    --auto-features=enabled \
    "${extra_config[@]}" \
    -Dbroadway_backend=true \
    -Dman=true \
    "../gtk"

  meson compile
}

package_gtk3() {
  depends+=("${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache")

  cd "${srcdir}/build-${CARCH}"

  DESTDIR="${pkgdir}" meson install

  # the hook needs this directory to exist, even if empty
  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/gtk-3.0/3.0.0/immodules
  for hook in gtk-query-immodules-3.0; do
    local hook_path="${srcdir}/${MINGW_PACKAGE_PREFIX}-${hook}.hook";
    cp "${srcdir}/${hook}.hook.in" "${hook_path}"
    sed -s "s|@MINGW_HOOK_TARGET_PREFIX@|${MINGW_PREFIX:1}|g" -i "${hook_path}"
    sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${hook_path}"
    sed -s "s|@MINGW_PACKAGE_PREFIX@|${MINGW_PACKAGE_PREFIX}|g" -i "${hook_path}"
    install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 "${hook_path}"
  done

  mv ${pkgdir}${MINGW_PREFIX}/bin/gtk-update-icon-cache "$srcdir"
  mv ${pkgdir}${MINGW_PREFIX}/share/man/man1/gtk-update-icon-cache.1 "$srcdir"

  install -Dm644 "${srcdir}/gtk/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}

package_gtk-update-icon-cache() {
  pkgdesc="GTK+ icon cache updater (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
           "${MINGW_PACKAGE_PREFIX}-librsvg"
           "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme")

  for hook in gtk-update-icon-cache; do
    local hook_path="${srcdir}/${MINGW_PACKAGE_PREFIX}-${hook}.hook";
    cp "${srcdir}/${hook}.hook.in" "${hook_path}"
    sed -s "s|@MINGW_HOOK_TARGET_PREFIX@|${MINGW_PREFIX:1}|g" -i "${hook_path}"
    sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${hook_path}"
    sed -s "s|@MINGW_PACKAGE_PREFIX@|${MINGW_PACKAGE_PREFIX}|g" -i "${hook_path}"
    install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 "${hook_path}"
  done

  for script in gtk-update-icon-cache; do
    local script_path="${srcdir}/${MINGW_PACKAGE_PREFIX}-${script}.script";
    cp "${srcdir}/${script}.script.in" "${script_path}"
    sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${script_path}"
    install -Dt "$pkgdir/usr/share/libalpm/scripts" -m644 "${script_path}"
  done

  install -Dt "${pkgdir}${MINGW_PREFIX}/bin" gtk-update-icon-cache
  install -Dt "${pkgdir}${MINGW_PREFIX}/share/man/man1" gtk-update-icon-cache.1
  cp ${pkgdir}${MINGW_PREFIX}/bin/gtk-update-icon-cache{,-3.0}.exe
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
